﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;
using Utilities.Security;

public partial class Common_ctrRegister : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Alert1.HideMessage();
     
        if (!IsPostBack)
        {
          
            ValidateControl();
            BindDataWhenEdit();
        }
    }

  
    private void ValidateControl()
    {
        int nCurrentLanguage = Support.GetCurrentLanguage ();
        nCurrentLanguage = nCurrentLanguage - 1;
        lblConfirmPassword.Text = MultipleLanguage.LBL_CONFIRM_PASSWORD[nCurrentLanguage];
        lblEmail.Text = MultipleLanguage.LBL_EMAIL[nCurrentLanguage];
        lblPassword.Text = MultipleLanguage.LBL_PASSWORD[nCurrentLanguage];
        lblRegister.Text = MultipleLanguage.REGISTRATION[nCurrentLanguage ];  
        lblUsername.Text = MultipleLanguage.LBL_FULLNAME[nCurrentLanguage];
        btnSave.Text = MultipleLanguage.BTN_SAVE[nCurrentLanguage];
        rfvUsername.ErrorMessage = MultipleLanguage.USERNAME_EMPTY[nCurrentLanguage];
        rfvEmail.ErrorMessage = MultipleLanguage.EMAIL_EMPTY[nCurrentLanguage];
        rfvPassword.ErrorMessage = MultipleLanguage.PASSWORD_EMPTY[nCurrentLanguage];
        revEmail.ErrorMessage = MultipleLanguage.EMAIL_WRONG[nCurrentLanguage];
        revPassword.ErrorMessage = MultipleLanguage.PASSWORD_SPACE_REMOVE[nCurrentLanguage];
        cpvPassword.ErrorMessage = MultipleLanguage.PASSWORD_NOT_SAME_CONFIRM_PASSWORD[nCurrentLanguage];
        lblUserInfo.Text = MultipleLanguage.USER_INFO[nCurrentLanguage];
        StaticPages objTerm = new StaticPages();
        int nTermID = objTerm.GetIDbyPageCode("dang-ky-thanh-vien");
        objTerm.LoadById(nTermID);
        switch (nCurrentLanguage)
        { 
            case 0:
                lrtTerm.Text = objTerm.Data.PageContent1;  
                break;
            case 1:
                lrtTerm.Text = objTerm.Data.PageContent2;  
                break;
            case 2:
                lrtTerm.Text = objTerm.Data.PageContent3;  
                break;
        }
    }

    private void BindDataWhenEdit()
    {
        string sAction = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        try
        {
            if (sAction == Config.ACTION_EDIT)
            {
                int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                if (nID > 0)
                {
                    Users objUser = new Users();
                    objUser.LoadById(nID);
                    txtEmail.Text = ConvertData.ConvertToString(objUser.Data.Email);
                    txtUserName.Text = ConvertData.ConvertToString(objUser.Data.Username);
                

                
                }
            }
        }
        catch
        {
            string sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_USER;
            Response.Redirect(sLink);
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage()-1;
        Users objUser = new Users();
        objUser.Data.Email = txtEmail.Text.Trim();
        objUser.Data.Username = txtUserName.Text.Trim();
        objUser.Data.RoleID = 3;
        objUser.Data.Status = EnumeType.INACTIVE;
        objUser.Data.CreatedDate = ConvertData.ConvertDatetimeToInt(DateTime.Today);  

        ClsEncrypt objEnCrypt = new ClsEncrypt();
        objUser.Data.Password = objEnCrypt.Encrypt(txtPassword.Text);

        int nResult = 0;

                nResult = objUser.Insert();
                if (nResult > 0)
                {
                    Alert1.ShowMessage(1, MessagesAlert.INSERT_SUCCESSFULL[nDefaultLanguage]);
                }
                else Alert1.ShowMessage(2, MessagesAlert.INSERT_UNSUCCESSFUL[nDefaultLanguage]);

       
    }
}
